<?php
class FabricApp extends MemberbaseApp
{
	var $_fabric_mod;
	
    function __construct()
    {
        $this->FabricApp();
    }
	
    function FabricApp()
    {
        parent::__construct();
		$this->check_powers(USER_ADMIN);
		$this->_fabric_mod =& m('fabric');
    }
	
	function index()
	{
		$fabric_list = $this->_fabric_mod->findAll();
		$this->assign('fabric_list', $fabric_list);
		$this->display('fabric.index.html');
		$this->_curlocal(Lang::get('fabric_manage'));
	}
	
	function _assign_form()
	{
		$fabric = array(
			'if_show' => 1,
		);
		$this->assign('fabric', $fabric);
	}
	
	function add()
	{
		if (!IS_POST) {
			$this->_assign_form();
			$this->display('fabric.form.html');
			$this->_curlocal(Lang::get('fabric_manage'), 'index.php?app=fabric', Lang::get('add_new_fabric'));
		} else {
			$fabric_name = empty($_POST['fabric_name']) ? null : trim($_POST['fabric_name']);
			$description = empty($_POST['description']) ? null : trim($_POST['description']);
			$if_show = empty($_POST['if_show']) ? 0 : intval($_POST['if_show']);
			
			if (!$fabric_name) {
				$this->show_warning('fabric_name_not_empty');
				return;
			}
			if (!$this->_fabric_mod->unique($fabric_name)) {
				$this->show_warning('fabric_name_is_exist');
				return;
			}
			
			$data = array(
				'fabric_name' => $fabric_name,
				'description' => $description,
				'if_show' => $if_show
			);
			$fabric_id = $this->_fabric_mod->add($data);
			if (!$fabric_id) {
				$this->_error($this->_fabric_mod->get_error());
				return;
			}
			$this->show_message('add_new_fabric_success', Lang::get('fabric_manage'), 'index.php?app=fabric');
		}
	}
	
	function edit()
	{
		$id = empty($_GET['id']) ? 0 : intval($_GET['id']);
		if (!$id) {
			$this->show_warning('fabric_not_exist');
			return;
		}
		$fabric = $this->_fabric_mod->get_info($id);
		if (!$fabric) {
			$this->show_warning('fabric_not_exist');
			return;
		}
		if (!IS_POST) {
			$this->assign('fabric', $fabric);
			$this->display('fabric.form.html');
			$this->_curlocal(Lang::get('fabric_manage'), 'index.php?app=fabric', Lang::get('edit_fabric'));
		} else {
			$fabric_name = empty($_POST['fabric_name']) ? null : trim($_POST['fabric_name']);
			$description = empty($_POST['description']) ? null : trim($_POST['description']);
			$if_show = empty($_POST['if_show']) ? 0 : intval($_POST['if_show']);
			
			if (!$fabric_name) {
				$this->show_warning('fabric_name_not_empty');
				return;
			}
			if (!$this->_fabric_mod->unique($fabric_name, $id)) {
				$this->show_warning('fabric_name_is_exist');
				return;
			}
			
			$data = array(
				'fabric_name' => $fabric_name,
				'description' => $description,
				'if_show' => $if_show
			);
			$this->_fabric_mod->edit($id, $data);
			if ($this->_fabric_mod->get_error()) {
				$this->_error($this->_fabric_mod->get_error());
				return;
			}
			$this->show_message('edit_fabric_success', Lang::get('fabric_manage'), 'index.php?app=fabric');
		}
	}
	
	function check_fabric()
	{
		$fabric_name = empty($_GET['fabric_name']) ? null : trim($_GET['fabric_name']);
		$id = empty($_GET['id']) ? 0 : intval($_GET['id']);
		if (!$fabric_name) {
			echo ecm_json_encode(false);
			return;
		}
		echo ecm_json_encode($this->_fabric_mod->unique($fabric_name, $id));
	}
}
?>